// pages/diary/diary.js
import { Diary } from './diary-model.js';
var diary = new Diary();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pets_id: 0,
    editmodel: 0,
    loadingHidden: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id;
    this.data.id = id;
    this._loadData();
  },

  onShow: function (options) {
    this._loadData();
  },

  _loadData: function () {
    var that = this;

    if (this.data.id > 0) {
      diary.getDiaryOneList(this.data.id, (res) => {
        that.setData({
          loadingHidden: true,
          diaryList: res.diary,
          pets_id: this.data.id
        })
        wx.setNavigationBarTitle({
          title: res.name + '的成长日记',
        })
      })
    } else {
      diary.getDiaryList((res) => {
        that.setData({
          loadingHidden: true,
          diaryList: res
        })
      })
    }

  },

  deleteTap: function(e) {
    var id = diary.getDataSet(e, 'id');
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定要删除吗？',
      success: function (sm) {
        if (sm.confirm) {
          diary.delDiary(id, (res) => {
            if(res.code) {
              diary.getDiaryOneList(that.data.id, (res) => {
                that.setData({
                  diaryList: res.diary,
                })
              })
            }else{
              wx.showToast({
                title: res.error,
                icon: 'none',
                duration: 2000,
              })
            }
          })
        } else if (sm.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  previewImage: function (e) {
    var that = this,
      index = diary.getDataSet(e, 'index'),
      pictures = new Array(this.data.diaryList[index].avatar);
    wx.previewImage({
      current: pictures[index],
      urls: pictures
    })
  },

  editDiaryTap: function (e) {
    var id = diary.getDataSet(e, 'id');
    wx.navigateTo({
      url: '../edit-diary/edit-diary?id=' + id,
    })
  },

  delDiaryTap: function (e) {
    var that = this
    wx.showModal({
      title: '操作提示',
      content: '确定删除日记吗？',
      success: function (res) {
        if (res.confirm) {
          var id = diary.getDataSet(e, 'id');
          diary.delDiary(id, (res) => {
            if (res.code) {
              wx.showToast({
                title: res.info,
                icon: 'success',
                duration: 2000,
              })
              that._loadData();
            } else {
              wx.showToast({
                title: res.error,
                icon: 'none',
                duration: 2000,
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  editTap: function(e) {
    this.setData({
      editmodel: 1,
    })
  },

  cancelEditTap: function (e) {
    this.setData({
      editmodel: 0,
    })
  },

  createDiaryTap: function(e) {
    var id = diary.getDataSet(e, 'id');
    wx.navigateTo({
      url: '../create-diary/create-diary?id=' + id,
    })
  },

  // 跳转到写日记
  onWriteDiary: function (e) {
    wx.navigateTo({
      url: '../write-diary/write-diary',
    })
  },
})